package com.ucar.train.service.impl;

import com.ucar.train.ServerResponse;
import com.ucar.train.dao.CarMapper;
import com.ucar.train.dao.StoreCarMappingMapper;
import com.ucar.train.dao.StoreMapper;
import com.ucar.train.dto.CarDTO;
import com.ucar.train.dto.StoreCarMappingDTO;
import com.ucar.train.dto.StoreDTO;
import com.ucar.train.service.StoreService;
import com.ucar.train.utils.JodaUtils;
import com.ucar.train.vo.CarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.mail.Store;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zjm
 * @date 2019/8/13
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private CarServiceImp carServiceImp;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private StoreCarMappingMapper storeCarMappingMapper;

    @Override
    public StoreDTO selectStorebyStoreId(String storeId) {
        return storeMapper.selectStorebyStoreId(storeId);
    }

    @Override
    public List<CarDTO> selectCarByStoreId(String storeId) {
          return  carServiceImp.selectCarByStoreId(storeId);
    }



    /**
    * @Description
    * @author Helingang
    * @param
    * @createDate 2019/8/15
    **/
    @Override
    public List<String>schedulCarByStoreId(String storeId, String carType, Integer carNumber)
    {
        //  根据车的类型查出除当前门店以外的车辆,返回list<car>
        List<CarDTO> carList = selectCarByCarTypeNotInCurrentStore(storeId, carType);
        List<String> carIdList=new ArrayList<>(carNumber);
        if (carList.size()>=carNumber){
            for (int i = 0; i < carNumber;i++){
                carIdList.add(carList.get(i).getCarId());
            }
        }else {
            //调度的车辆的数量不能满足的情况
            return null;
        }
        //再调度需要的车辆的数量,将list<car>中调度的carid修改为当前门店id
        carServiceImp.updateCarOfStoreIdAfterSchedul(carIdList,storeId);
        //  返回调度的车辆的Id
            return carIdList;
    }


   public List<StoreDTO> selectAllStore(){
      return storeMapper.selectAllStore();
   }


   /**
   * @Description
   * @author Helingang
   * @param
   * @createDate 2019/8/15
   **/
    @Override
    public Integer updateCarOfStoreIdAfterSchedul(List<String> carIdList, String storeId) {
       return carServiceImp.updateCarOfStoreIdAfterSchedul(carIdList,storeId);
    }

    /**
    * @Description
    * @author Helingang
    * @param
    * @createDate 2019/8/15
    **/
    private List<CarDTO>selectCarByCarTypeNotInCurrentStore(String storeId, String carType)
    {
        return storeMapper.selectCarByCarTypeNotInCurrentStore(storeId, carType);
    }

    @Override
    public List<StoreCarMappingDTO> selectCarInfoByStoreId(String storeCode) {

        return storeCarMappingMapper.selectCarInfoByStoreCode(storeCode);
    }

    @Override
    public ServerResponse<ModelAndView> addStore(StoreDTO storeDTO) {
        ModelAndView modelAndView = new ModelAndView();
//        if (StringUtils.isNotBlank(storeDTO.getName())
//                && StringUtils.isNotBlank(storeDTO.getPosition())
//                && StringUtils.isNotBlank(storeDTO.getStoreCode())) {
        storeMapper.insert(storeDTO);
//            storeMapper.insertSelective(storeDTO);
        modelAndView.setViewName("index");
        return ServerResponse.createBySuccess("添加门店成功", modelAndView);
//        }
//        return ServerResponse.createBySuccess("添加门店成功", modelAndView);
    }

    @Override
    public ServerResponse<ModelAndView> addCar(CarDTO carDTO, String storeCode, BigDecimal cost) {
        ModelAndView modelAndView = new ModelAndView();
//        if (StringUtils.isNotBlank(carDTO.getType())
//                && StringUtils.isNotBlank(carDTO.getCarId())
//                && StringUtils.isNotBlank(carDTO.getType())
//                && StringUtils.isNotBlank(carDTO.getCarNum())
//                && StringUtils.isNotBlank(storeCode)) {
        StoreCarMappingDTO storeCarMappingDTO = new StoreCarMappingDTO();
        storeCarMappingDTO.setCarId(carDTO.getCarId());

        storeCarMappingDTO.setStoreCode(storeCode);
        storeCarMappingDTO.setCost(cost);
        storeCarMappingDTO.setCreateTime(JodaUtils.nowAsDate());
        storeCarMappingDTO.setModifyTime(JodaUtils.nowAsDate());

        storeCarMappingMapper.insert(storeCarMappingDTO);
        carMapper.insert(carDTO);
        modelAndView.addObject("add-car-success");
        modelAndView.setViewName("index");
        return ServerResponse.createBySuccess("添加车辆成功", modelAndView);
//        }
//        return ServerResponse.createByErrorMessage("车辆信息不能为空");
    }

    /**
     * @Author zhoujinmu
     * @Description 根据门店编号和车辆类型获取车辆价格
     * @Date 2019/8/19 13:33
     */
    @Override
    public BigDecimal getCarCostByCarTypeAndStoreCode(String storeCode, String carType) {

        BigDecimal cost = storeCarMappingMapper.getCostByStoreCodeAndCarId(storeCode,carType);
        System.out.println(cost);
        return cost;
    }
}
